package org.dormi.learn.definition;

import java.util.Collection;
import java.util.Iterator;
import java.util.TreeSet;

/**
 * @author : dormi330
 * @since : 2021/1/28/0028
 * 顶点邻接表
 */
public class Adjacence implements Iterable<Edge> {
    public final Vertex src;
    private final TreeSet<Edge> edges;

    public Adjacence(Vertex src) {
        this.src = src;
        this.edges = new TreeSet<>();
    }

    public void addEdge(Vertex v, int weight) {
        edges.add(new Edge(this.src, v, weight));
    }

    public int size() {
        return edges.size();
    }

    @Override
    public Iterator<Edge> iterator() {
        return edges.iterator();
    }

    public void removeAll(Collection<Edge> toRemove) {
        if (null == toRemove || toRemove.isEmpty()) {
            return;
        }
        edges.removeAll(toRemove);
    }
}
